package cn.jekun.shop.web.service;

import java.io.File;
import java.net.URL;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.DateJsonValueProcessor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import test.Main;
import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.codegen.FreemarkerHelper;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpProductHis;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.service.ConfigService;
import cn.jekun.component.core.service.gen.RsSmsService;
import cn.jekun.shop.common.service.ShopBaseService;
import cn.jekun.shop.web.bean.HistoryGoods;
/**
 * 静态化类
 * @author ytl
 *
 */
@Service(FreeMarkerService.BEAN_NAME)
public class FreeMarkerService extends ShopBaseService{
	public static final String BEAN_NAME = "cn.jekun.shop.web.service.FreeMarkerService";
	
	static ConfigService configService;
	
	
	@Override
	public String getNamespace() {
		return "FREEMARKER";
	}
	
	
	
	/**
	 * 生成首页静态页面
	 * @param root
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "static-access","rawtypes" })
	public static void createStaticHtml() throws Exception{
		
		String projectRoot = null;
		URL classUrl = Main.class.getProtectionDomain().getCodeSource().getLocation();//.../source/sys/target/classes
		if("file".equals(classUrl.getProtocol())){
			projectRoot = new File(classUrl.getPath()).getParentFile().getParent();//.../source/sys
		}
		if(projectRoot == null){
			throw new NullPointerException("没有找到项目路径");
		}
		final String root = projectRoot;
		
		Map context = new HashMap();
		
		context.put("ctx", "/shop");
		
		context.put("fileUploadUrl",configService.getFileUploadUrl());
		
		context.put("fileDownUrl",configService.getFileDownloadUrl());
		
		context.put("fileOpenUrl",configService.getFileOpenUrl());
		
		context.put("headPath", "/src/main/webapp/new/web/public/head.html");
		
		File outFile=null;
		
		String templatePath="/ftl/";
		
		String templateName=null;
		
		//html
		
		outFile=new File(root+"/src/main/webapp/new/web/index.html");
		
		templateName="curd/index.ftl";
		
		FreemarkerHelper.produce(context,"UTF-8", outFile, templatePath, templateName,"UTF-8");
		
		//登录页
		outFile=new File(root+"/src/main/webapp/new/web/login.html");
		
		templateName="curd/login.ftl";
		
		FreemarkerHelper.produce(context,"UTF-8", outFile, templatePath, templateName,"UTF-8");
		
		
	}
}
